package edu.swust.graduation_project.eauction.activity

import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import edu.swust.graduation_project.eauction.R
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException

class SignInActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_in)
        val mEtUsername = findViewById<EditText>(R.id.et_username)
        val mEtPassword = findViewById<EditText>(R.id.et_password)
        val mEtConPassword = findViewById<EditText>(R.id.et_copassword)
        val mBtnSign = findViewById<Button>(R.id.btn_complete_sign_in)
        mBtnSign.setOnClickListener {
            if (mEtPassword.text.toString() != mEtConPassword.text.toString()) {
                Toast.makeText(this, "输入密码不一致，注册失败", Toast.LENGTH_SHORT).show()
            } else if (mEtUsername.text.toString().isEmpty() ||
                mEtPassword.text.toString().isEmpty() || mEtConPassword.text.toString().isEmpty()
            ) {
                Toast.makeText(this, "信息填写不完整，注册失败", Toast.LENGTH_SHORT).show()
            } else {
                Thread {
                    Looper.prepare()
                    signInMysql(mEtUsername, mEtPassword)
                }.start()
            }
        }
    }

    private fun signInMysql(mEtUsername: EditText, mEtPassword: EditText) {
        val mEtUsername = mEtUsername.text.toString().trim()
        val mEtPassword = mEtPassword.text.toString().trim()
        val sqlInsert = " INSERT INTO `test_case`.`Account` " +
                "(`user_name`, `password`, `isroot`, `user_phone`, `user_email`) " +
                "VALUES ('$mEtUsername', '$mEtPassword', 0, NULL, NULL)"
        val sqlSelect = "SELECT * FROM `Account` WHERE " +
                "`user_name` = '$mEtUsername' "
        val cn: Connection
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver")
            //建立连接
            cn = DriverManager.getConnection(
                "*e",
                "*", "*"
            )
            val ps = cn.createStatement()
            val resultSet = ps!!.executeQuery(sqlSelect)
            if (resultSet.next()) {
                Log.d("main", "注册数据库失败，该用户名已注册")
                Toast.makeText(this@SignInActivity, "注册数据库失败，该用户名已注册", Toast.LENGTH_SHORT).show()
            } else {
                try {
                    ps.execute(sqlInsert)
                } catch (e: ClassNotFoundException) {
                    Toast.makeText(this@SignInActivity, "$e", Toast.LENGTH_SHORT).show()
                    e.printStackTrace()
                } catch (e: SQLException) {
                    Toast.makeText(this@SignInActivity, "$e", Toast.LENGTH_SHORT).show()
                    e.printStackTrace()
                }
                ps.close()
                cn?.close()
                Log.d("main", "注册数据库成功")
                Toast.makeText(this@SignInActivity, "注册数据库成功", Toast.LENGTH_SHORT).show()
                finish()
            }
        } catch (e: ClassNotFoundException) {
            Toast.makeText(this@SignInActivity, "$e", Toast.LENGTH_SHORT).show()
            e.printStackTrace()
        } catch (e: SQLException) {
            Toast.makeText(this@SignInActivity, "$e", Toast.LENGTH_SHORT).show()
            e.printStackTrace()
        }
    }
}